﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Ls.Web.Admin.Areas.Sys.Models;
using Ls.Web.Framework;
using Ls.Web.Admin.Models;
using Microsoft.AspNetCore.Authorization;
using Ls.Utils;

namespace Ls.Web.Admin.Controllers
{
    [Authorize]
    public class UserController : BaseController
    {
        UserService userService = new UserService();

        /// <summary>
        /// 验证密码是否正确
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult VerifyPass()
        {
            string password = Request.Form["password"]; 
          
            long userId = CommonHelper.ToInt64(AuthHelper.GetUserid());
            dynamic user = userService.Single("user_id, userpwd,salt", new { user_id = userId, user_status = 0 });
            if (user != null && user.userpwd == CryptoHelper.MD5(password + user.salt))
            { 
                return GetSuccessResult("密码正确！");
            }
            else
            {
                return GetErrorResult("密码错误！");
            }
        }
        [HttpPost]
        public IActionResult Changepass() {
            string oldpass = Request.Form["oldpass"];
            string newpass = Request.Form["newpass"];
            long userId = CommonHelper.ToInt64(AuthHelper.GetUserid());
            dynamic user = userService.Single("user_id, userpwd,salt", new { user_id = userId });
            if (user != null && user.userpwd == CryptoHelper.MD5(oldpass + user.salt))
            {
                userService.Update(new { userpwd = CryptoHelper.MD5(newpass + user.salt) },new { user_id = userId });
                return GetSuccessResult("修改密码成功！");
            }
            else
            {
                return GetErrorResult("密码错误！");
            }

        }
    }
}